//
//  TaskSortViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/16.
//  Copyright © 2017年 cdnunion. All rights reserved.
//

#import "TaskSortViewController.h"
#import "TaskSortTableViewCell.h"
#import "SortListModel.h"
#import "AddTaskSortViewController.h"
#import "EditTaskSortViewController.h"


static NSString *tableViewCellIdentifier=@"tableViewCellIdentifier";
@interface TaskSortViewController ()<UITableViewDataSource,UITableViewDelegate,TaskSortTableViewCellDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *sortArr;

// 当前页
@property (nonatomic,assign)int currentPage;

@end

@implementation TaskSortViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.sortArr=[NSMutableArray array];

    [self addHeader];

    [self addFooter];

}

-(void)addHeader
{
    WEAKSELF;
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage=1;

        [self.sortArr removeAllObjects];

        [weakSelf getSortListData];

    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)addFooter
{
    WEAKSELF;
    MJRefreshAutoNormalFooter *footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage += 1;
        [weakSelf getSortListData];
    }];
    [footer setTitle:@"我是有底线的" forState:MJRefreshStateNoMoreData];
    footer.automaticallyHidden=YES;
    self.tableView.mj_footer=footer;
}

// 编辑分类
-(void)editTaskSortWithSortModel:(SortModel *)model cell:(TaskSortTableViewCell *)cell
{
    EditTaskSortViewController *vc=[EditTaskSortViewController new];
    vc.model=model;
    [self.navigationController pushViewController:vc animated:NO];
}

// 删除分类
-(void)deleteTaskSortWithSortModel:(SortModel *)model cell:(TaskSortTableViewCell *)cell
{
  UIAlertView *alert=[UIAlertView alertViewWithTitle:@"提示" message:@"你确定要删除它吗？" cancelButtonTitle:@"取消" didDismissBlock:^(UIAlertView *alertView,NSInteger buttonIndex) {

        if (buttonIndex==0)
        {
            [API DeleteSortWithSortID:model.sortID success:^(id res) {

                [self addHeader];

                [self addFooter];

            } failure:^(NSInteger stateCode) {

            }];
        }
    } otherButtonTitles:@"确定", nil];
    [alert show];
}

-(void)addProjectBtn
{
    UIButton *addBtn=[AppUI createButtonWithSupeView:self.view andWithNormalImage:[UIImage imageNamed:@"add"] andWithSelectImage:[UIImage imageNamed:@"add"] andWithBtnAction:^{

        // 跳转到   分类添加  界面
        AddTaskSortViewController *vc=[AddTaskSortViewController new];
        [self.navigationController pushViewController:vc animated:NO];

    }];
    [self.view addSubview:addBtn];
    addBtn.frame=CGRectMake(APPW-kmargin*8,APPH-kmargin*10,kmargin*6,kmargin*6);
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title=@"分类";

    [self.view addSubview:self.tableView];

    [self addProjectBtn];

}

-(void)getSortListData
{
    WEAKSELF;
    [API GetSortListWithPer_page:KCountNumber page_num:[NSString stringWithFormat:@"%d",_currentPage] success:^(id res) {

        [weakSelf endRefresh:self.tableView array:self.sortArr];

        if ([res[@"rt"] isEqual:@(1)])
        {
            NSArray *array=res[@"rows"];
            for (NSDictionary *dic in array)
            {
                SortModel *model=[SortModel yy_modelWithDictionary:dic];
                [weakSelf.sortArr addObject:model];
            }

            if (self.currentPage<[res[@"page"][@"pages"] integerValue])
            {
                [weakSelf.tableView.mj_footer resetNoMoreData];
            }
            else
            {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        else
        {
           [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
        }

        [weakSelf.tableView reloadData];

    } failure:^(NSInteger stateCode) {

        [weakSelf requestFail:self.tableView array:self.sortArr];
    }];
}

#pragma mark 上下拉刷新
- (void)endRefresh:(UITableView *)tableView array:(NSMutableArray *)array
{
    if ([tableView.mj_header isRefreshing])
    {
        [array removeAllObjects];
        [tableView.mj_header endRefreshing];
    }
    if ([tableView.mj_footer isRefreshing])
    {
        [tableView.mj_footer endRefreshing];
    }
}

// 收到请求失败的通知
- (void)requestFail:(UITableView *)tableView array:(NSMutableArray *)array
{
    if ([tableView.mj_header isRefreshing])
    {
        [tableView.mj_header endRefreshing];
    }
    if ([tableView.mj_footer isRefreshing])
    {
        [tableView.mj_footer endRefreshing];
    }
    [array removeAllObjects];
    [tableView reloadData];
}


// tableView
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=BGColor;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.tableView registerClass:[TaskSortTableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sortArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TaskSortTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
    cell.delegate=self;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.sortModel=self.sortArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kmargin*9;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView=[UIView new];
    return footView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kmargin;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    SortModel *model=self.sortArr[indexPath.row];
     if (self.delegate&&[self.delegate respondsToSelector:@selector(selectTaskSortName:taskSortID:)])
    {
        [self.delegate selectTaskSortName:model.target_name taskSortID:model.sortID];
    }
    [self.navigationController popViewControllerAnimated:NO];
}

@end
